//
//  DHSettingViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2017/12/27.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHSettingViewController.h"
#import "DHSettingControllerCell.h"
#import "DHAccountSettingViewController.h"
#import "DHWebViewController.h"
#import <StoreKit/StoreKit.h>

@interface DHSettingViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *dataArr;

@end

@implementation DHSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"APP设置";
    self.view.backgroundColor = [UIColor whiteColor];
    
    if ([Login isLogin]) {
        self.dataArr = @[@[@"账号设置",@"通知设置"],@[@"用户指南",@"订阅会员权益说明",@"给DRESSHERE好评"],@[@"法律条款与隐私政策",@"关于DRESSHERE"]];
    }else{
        self.dataArr = @[@[@"通知设置"],@[@"用户指南",@"订阅会员权益说明",@"给DRESSHERE好评"],@[@"法律条款与隐私政策",@"关于DRESSHERE"]];
    }
    
    [self.tableView registerClass:[DHSettingControllerCell class] forCellReuseIdentifier:kDHSettingControllerCell];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArr[section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.dataArr count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHSettingControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHSettingControllerCell forIndexPath:indexPath];
    cell.titleLabel.text = self.dataArr[indexPath.section][indexPath.row];
    if (indexPath.row == [self.dataArr[indexPath.section] count] - 1) {
        cell.lineView.hidden = YES;
    }else{
        cell.lineView.hidden = NO;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if ([Login isLogin]) {
            if (indexPath.row == 1) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            }else if (indexPath.row == 0){
                DHAccountSettingViewController *vc = [[DHAccountSettingViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }else{
            if (indexPath.row == 0) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            }
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            DHWebViewController *webController = [[DHWebViewController alloc] init];
            webController.htmlString = [NSString stringWithFormat:@"%@user.html",kHost];
            webController.titleString = @"用户指南";
            [self.navigationController pushViewController:webController animated:YES];
        }else if (indexPath.row == 1){
            DHWebViewController *webController = [[DHWebViewController alloc] init];
            webController.htmlString = [NSString stringWithFormat:@"%@vip.html",kHost];
            webController.titleString = @"订阅会员权益说明";
            [self.navigationController pushViewController:webController animated:YES];
        }else if (indexPath.row == 2){// 评分
            if ([UIDevice systemVersion] < 11) {
                NSString  * nsStringToOpen = [NSString  stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",@"1329185718"];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];
            }else{
                [SKStoreReviewController requestReview];
            }
        }
    }else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            DHWebViewController *webController = [[DHWebViewController alloc] init];
            webController.htmlString = [NSString stringWithFormat:@"%@protocol.html",kHost];
            webController.titleString = @"法律条款与隐私政策";
            [self.navigationController pushViewController:webController animated:YES];
        }else if (indexPath.row == 1){
            DHWebViewController *webController = [[DHWebViewController alloc] init];
            webController.htmlString = [NSString stringWithFormat:@"%@about.html",kHost];
            webController.titleString = @"关于DRESSHERE";
            [self.navigationController pushViewController:webController animated:YES];
        }
    }
}

//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 13;//section头部高度
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 13)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}


@end
